package br.com.mestrefamiliar.familia;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class FamiliaDAOHibernate implements FamiliaDAO{
	
	private Session session;
	
	public void setSession(Session session){
		this.session = session;
	}

	@Override
	public void salvar(Familia familia) {
		this.session.saveOrUpdate(familia);
		
	}

	@Override
	public void excluir(Familia familia) {
		this.session.delete(familia);
		
	}

	@Override
	public Familia carregar(Integer codigo) {
		return (Familia) this.session.get(Familia.class, codigo);

	}

	@Override
	public Familia buscarPorLogin(String login) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Familia> listar() {
		return this.session.createCriteria(Familia.class).list();
	}

	@Override
	public boolean verificaSeCadastrado(String nome) {
		String hql = "select u from Familia u where u.email = :nome";
		Query consulta = this.session.createQuery(hql);
		consulta.setString("email", nome);
		if(consulta.list().size() > 0)
			return true;
		else
			return false;
	}

}
